from forest.resource import Resources, Resource, ValidationError
from forest.resources.common import translate_disk_name

class Filesystem(Resource):
    ''' Filesystem resources specify devices/partitions to format for the use of the target.
    
        Example::

            resrouces:
            - $: filesystem
              mount: /boot
              fs: ext4
              device: sda2

            - $: filesystem
              mount: swap
              fs: swap
              device: sda3

            - $: filesystem
              mount: /
              fs: ext4
              device: sda4

        .. describe:: mount

            path in the installroot to mount

        .. describe:: device

            device/partition to mount

        .. describe:: fs

            valid filesystem short name (ext3, ext4, swap)

        .. .. describe:: fsoptions

        ..    TODO: options for filesystems
    '''

    __register__ = "filesystem"

    def get_device(self):
        return translate_disk_name(self.config, self.value.get('device'))

    def get_id(self):
        return 'filesystem:'+self.value.get('mount')

    def get_type(self):
        return self.value.get('type')

    def get_mount(self):
        return self.value.get('mount')

    def validate(self):
        keys = ['type', 'mount', 'device']

        for k in self.value.iterkeys():
            if k in keys:
                continue
            raise ValidationError('Undefined filesystem key {}'.format(k))

        for necessary in keys:
            if necessary in self.value:
                continue
            raise ValidationError('Missing mandatory filesystem option {}. Please specify it.'.format(necessary))


